package cn.lyjuan.first.netty.demo._5_netty_heartbeat.server.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.net.InetSocketAddress;

public class ServerHeartHandler extends ChannelInboundHandlerAdapter
{


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception
    {
        if (IdleStateEvent.class.isInstance(evt))
        {
            super.userEventTriggered(ctx, evt);
            return;
        }

        IdleState state = ((IdleStateEvent) evt).state();
        // 服务器端如果触发客户端的读空闲，则报错
        if (state == IdleState.READER_IDLE)
        {
            InetSocketAddress host = (InetSocketAddress) ctx.channel().remoteAddress();
            throw new RuntimeException("idle error from: " + host.getHostName() + ":" + host.getPort());
        }
    }
}
